package com.timepies.activity;

import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

import com.timepies.R;
import com.timepies.domain.enums.MenuPies;
import com.timepies.view.ElencoPieList;
import com.timepies.view.LeftMenu;
import com.timepies.view.PieAbortButton;
import com.timepies.view.PieConfirmButton;
import com.timepies.view.PieDatePicker;

public class ElencoPieActivity extends Activity {
	private static final String TAG = ElencoPieActivity.class.getSimpleName();
	private Dialog editDialog = null;
	private LeftMenu leftMenu;
	private ElencoPieList elencoPieList;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_pie_elenco);

		elencoPieList = new ElencoPieList(this);

		leftMenu = new LeftMenu(this, MenuPies.titoli());

		Log.d(TAG, "onCreate complete!");

	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.mainlist_menu, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		Log.d(TAG, "MenuItem: " + item.getItemId());
		switch (item.getItemId()) {

		case R.id.action_settings:
			startActivity(new Intent(ElencoPieActivity.this,
					SettingsActivity.class));
			finish();
			break;
		case R.id.action_about:
			startActivity(new Intent(ElencoPieActivity.this,
					SplashActivity.class));
			finish();
			break;

		case R.id.action_add:
			Log.d(TAG, "buildDialog start");
			this.editDialog = new Dialog(this);
			this.editDialog
					.setContentView(R.layout.activity_pie_dettaglio_dialog);
			this.editDialog.setTitle("Il tuo evento speciale");
			this.editDialog.show();
			PieDatePicker.addTo(this);
			PieAbortButton.addTo(this);
			PieConfirmButton.addTo(this);
			break;

		default:
			leftMenu.select(item);

		}

		return true;
	}

	@Override
	public void onBackPressed() {
		Builder finishDialog = new Builder(this);
		finishDialog.setTitle("Abbandona Pie");
		finishDialog.setMessage("Sicuro di voler uscire?");
		finishDialog.setPositiveButton("Si",
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int button) {
						ElencoPieActivity.this.finish();
					}
				});
		finishDialog.setNegativeButton("No", null).show();
	}

	public Dialog getEditDialog() {
		return editDialog;
	}

	public ElencoPieList getElencoPieList() {
		return elencoPieList;
	}

}